#include "../include/test.h"
#include "../include/Hash.h"
#include "../include/vfs_errorcodes.h"
#include <stdio.h>
#include <string.h>
void recsearch(struct Node* temp,FILE *fp2);
    char * list(char *path,int flag,char *dest_path){
	
        struct Node* temp;
        int file_descriptor_no=search_from_hash_table(path);
        
        FILE * fp2;
	if(flag!=0 && flag!=1) {
	strcpy(error,"listdir_FAILURE ");
	strcat(error,ERR_VFS_LISTDIR_02);
	return error;
	}

        if((fp2=fopen(dest_path,"w+"))==NULL)
  	{
	 strcpy(error,"listdir_FAILURE ");
	 strcat(error,ERR_VFS_LISTDIR_04);
   	// printf("\nCannot open the file\n");
   	 return error;
  	}
        
        if(file_descriptor_no!=-1)
        { 
        temp = searchNode(file_descriptor_no);
        temp = temp->child;
        
        if(flag==1)
        {
          
           recsearch(temp,fp2);

        }

        else
        {
        while(temp != NULL){

            
            if(header.fd[temp->file_descriptor_no].file_type=='d')
	    {
              //printf(" \033[32;1m %s \033[0m  ",header.fd[temp->file_descriptor_no].file_name);
              fputs(header.fd[temp->file_descriptor_no].file_name,fp2);
              fputs("\n",fp2);
	    }	
            else
             { 
            fputs(header.fd[temp->file_descriptor_no].file_name,fp2);
            fputs("\n",fp2);
            }
            temp = temp->sibling;
        }
        
       }
          strcpy(error,"listdir_SUCCESS");
      } 
      else
     {  
	 strcpy(error,"listdir_FAILURE ");
	 strcat(error,ERR_VFS_LISTDIR_01);
        // printf("\nPath is not valid\n");
     }
      
    
   // printf("\n");
    fclose(fp2);
	return error;
   }
   void recsearch(struct Node* temp,FILE *fp2)
    {
      //printf("\nin recsearch %s\n",header.fd[temp->sibling->file_descriptor_no].path);
         if(temp==NULL)
          return;
       
        if(header.fd[temp->file_descriptor_no].file_type=='d')
        {
        			
              		//printf(" \033[32;1m %s \033[0m   ",header.fd[temp->file_descriptor_no].path);
              		
                        fputs(header.fd[temp->file_descriptor_no].path,fp2); 
                        
			fputs("\n",fp2);
		
	}
            	else
         	{
     		//printf(" %s ",header.fd[temp->file_descriptor_no].path); 
                         fputs(header.fd[temp->file_descriptor_no].path,fp2);
			fputs("\n",fp2);
		}
		
			
			
      	  recsearch(temp->sibling,fp2);
      	 
         recsearch(temp->child,fp2);
       		


    }
